package com.software.software.controler;

import com.software.software.model.server.faceSever;
import com.software.software.mapper.UserMapper;
import com.software.software.model.userInfo;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

@RestController
public class faceController {
    @Resource
    private UserMapper userMapper;

    @RequestMapping("/checkface")
    public String checkface(@RequestBody String file, HttpSession session) throws Exception {
        String facetoken = faceSever.isface(file);
        userInfo userInfo = (userInfo)session.getAttribute("userInfo");
        String userid=""+userInfo.userId;//////////////////////////////////
        if (facetoken != null) {
            if (!faceSever.haveFaceSet(userid)) {
                return "没有人脸信息";
            }
            if (!faceSever.search(facetoken, userid)) {
                return "人脸对比失败";
            }
            return "识别成功";
        } else {
            return "请确保一个人在镜头中";
        }
    }

    @RequestMapping("/addfaceinfo")
    public String addfaceinfo(@RequestBody String file, HttpSession session) throws Exception {
//        File file = new File("D:\\qqfile\\qqpicture\\oldest.jpg");
        userInfo userInfo = (userInfo)session.getAttribute("userInfo");
        String userid=""+userInfo.userId;//////////////////////
        System.out.println(file);
        String facetoken = faceSever.isface(file);
        System.out.println(facetoken);
        if (facetoken != null) {
            if (!faceSever.haveFaceSet(userid)) {
                faceSever.creatFaceSet(userid);
            }
            if (!faceSever.addFace(userid, facetoken)) {
                return "添加失败";
            }
        } else {
            return "请确保一个人在镜头中";
        }
        Map<String, Integer> m=new HashMap<>();
        m.put("userId",userInfo.userId);
        m.put("state",1);
        userMapper.changeHaveFace(m);
        return "添加成功";
    }

    @RequestMapping("/deletefaceinfo")
    public String deletefaceinfo(HttpSession session) throws Exception {
        userInfo userInfo = (userInfo)session.getAttribute("userInfo");
        String userid=""+userInfo.userId;
        System.out.println("a");
        if(faceSever.deleteFaceSet(userid)) {
            System.out.println("c");
            Map<String, Integer> m = new HashMap<>();
            m.put("userId", userInfo.userId);
            m.put("state", 0);
            userMapper.changeHaveFace(m);
            return "删除成功";
        }
        else {
            System.out.println("b");
            return "没有人脸信息";
        }
    }

}
